<?php

/**
 * TemplateController
 * 
 * @author Andrey Mukhin
 * @version 2.0
 */

require_once 'Zend/Controller/Action.php';

class Cms_TemplateController extends Zend_Controller_Action {
	/**
	 * The default action - show the home page
	 */
	public function indexAction() {
		CmsAuth::check();
		if (NULL === $id = $this->getRequest()->getParam('menu_id')) {
			throw new Zend_Controller_Action_Exception('Required param "Menu_Id"', 404);
		}
		
		
		$menuItem = MF_Main::getInstance()->Menu->getPointById($id);
		if (!CmsAccess::checkMenu($menuItem)) {
			$this->render('no-access');
		} else {
			switch ($menuItem->menu_type_id) {
				case '1':
					$this->view->page = $menuItem->page;
					$this->render('page_edit');
					break;
					
				case '3':
					$this->view->direct_url = $menuItem->direct_url;
					$this->render('url_edit');
				break;
				
				default:
								
					
					break;
			}
		}
		
		
		$this->_helper->layout->disableLayout();
	}

	public function seoAction() {
		CmsAuth::check();
		if (NULL === $id = $this->getRequest()->getParam('id')) {
			throw new Zend_Controller_Action_Exception('Required param "Id"', 404);
		}
		
		$page = MF_Main::getInstance()->Page->getById($id);
		
		$this->view->page = $page;
		
		if ($this->_request->isPost()) {
			$post_data = $this->_request->getPost();
			
			$page->title = trim($post_data['title']);
			$page->description = trim($post_data['description']);
			$page->keywords = trim($post_data['keywords']);
			
			$this->renderScript('exit.phtml');
			return true;
		}
	}
	
	
	public function imageAction() {
		CmsAuth::check();
		if (NULL === $file = $this->getRequest()->getParam('file')) {
			throw new Zend_Controller_Action_Exception('Required param "Menu_Id"', 404);
		}
		
		$main = MF_Main::getInstance();
		$main->Utils->File($main->Utils->fixDirPath($main->Template->preview_dir).$file)->Push();

		
		$this->_helper->layout->disableLayout();
	}
	
	public function contentdisableAction() {
		CmsAuth::check();
		
		if (NULL === $page_id = $this->getRequest()->getParam('page_id')) {
			throw new Zend_Controller_Action_Exception('Required param "content_id"', 404);
		}
		
		if (NULL === $content_key = $this->getRequest()->getParam('content_key')) {
			throw new Zend_Controller_Action_Exception('Required param "content_key"', 404);
		}
		
		$contentItem = MF_Main::getInstance()->Page->getById($page_id)->getContent($content_key);
		$newVal = $contentItem->hidden_f=='1' ? '0' : '1';
		$contentItem->hidden_f = $newVal;
		$this->view->result = $newVal;
		$this->_helper->layout->disableLayout();
	}
}
?>